<?php
error_reporting(0);
@header('Content-Type: text/html; charset=UTF-8');
$do=isset($_GET['do'])?$_GET['do']:'0';
if(file_exists('install.lock')){
	$installed=true;
	$do='0';
}

function checkfunc($f,$m = false) {
	if (function_exists($f)) {
		return '<font color="green">可用</font>';
	} else {
		if ($m == false) {
			return '<font color="black">不支持</font>';
		} else {
			return '<font color="red">不支持</font>';
		}
	}
}

function checkclass($f,$m = false) {
	if (class_exists($f)) {
		return '<font color="green">可用</font>';
	} else {
		if ($m == false) {
			return '<font color="black">不支持</font>';
		} else {
			return '<font color="red">不支持</font>';
		}
	}
}

function random($length, $numeric = 0) {
	$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
	$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
	$hash = '';
	$max = strlen($seed) - 1;
	for($i = 0; $i < $length; $i++) {
		$hash .= $seed{mt_rand(0, $max)};
	}
	return $hash;
}
?>
<?php
if($do=='0'){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<link rel="stylesheet" href="https://bootswatch.com/3/paper/bootstrap.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>BR授权系统-在线安装</title>
</head>
<body>
<div class="container" style="padding-top:55px;">
<div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 center-block text-center" style="float: none;">
<div class="panel panel-primary">
<div class="panel-heading" style="background: linear-gradient(to right,#8ae68a,#5ccdde,#b221ff);"><font color="#000000">Birth授权系统在线安装</font></div>
<div class="panel-body">
<font color="#00FFFF">不</font><font color="#05FAFF">妥</font><font color="#0AF5FF">协</font><font color="#0FF0FF">，</font><font color="#14EBFF">不</font><font color="#19E6FF">逐</font><font color="#1EE1FF">流</font><font color="#23DCFF">；</font><font color="#28D7FF">随</font><font color="#2DD2FF">性</font><font color="#32CDFF">而</font><font color="#37C8FF">不</font><font color="#3CC3FF">失</font><font color="#41BEFF">个</font><font color="#46B9FF">性</font><font color="#4BB4FF">，</font><font color="#50AFFF">有</font><font color="#55AAFF">设</font><font color="#5AA5FF">计</font><font color="#5FA0FF">而</font><font color="#649BFF">不</font><font color="#6996FF">漏</font><font color="#6E91FF">痕</font><font color="#738CFF">迹</font><font color="#7887FF">；</font><font color="#7D82FF">缘</font><font color="#827DFF">浅</font><font color="#8778FF">支</font><font color="#8C73FF">付</font><font color="#916EFF">系统</font><font color="#9669FF">，</font><font color="#9B64FF">繁</font><font color="#A060FF">华</font><font color="#A560FF">阅</font><font color="#AA60FF">尽</font><font color="#AF60FF">处</font><font color="#B460FF">，</font><font color="#B960FF">简</font><font color="#BE60FF">约</font><font color="#C360FF">不</font><font color="#C860FF">简</font><font color="#CD60FF">单</font><font color="#D260FF">！
</font></p><span class="label label-info">亮而不躁</span></a> <span class="label label-primary">丽而不曲</span> </a> <span class="label label-danger">简洁明了</span></a> <span class="label label-warning">多程序化</span></a> <span class="label label-success">运行流畅</span></a> <span class="label label-warning">安全稳定</span>
</div>
<div class="panel-footer text-center">
<a href="index.php?do=1" class="btn btn-success">开始全新体验</a>
</div>
  </div>
  </div>
  <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 center-block text-center" style="float: none;">
<div class="panel panel-primary">
<div class="panel-heading" style="background: linear-gradient(to right,#8ae68a,#5ccdde,#b221ff);"><font color="#000000">Birth授权系统-版本更新内容</font></div>
<div class="panel-body">
  <?php
        include_once('../readme.html');
        ?>
  </div>
</div>
</div>
</body>

</html>
<?php }else{?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <title>BR授权系统-在线安装</title>
    <link href="https://bootswatch.com/3/paper/bootstrap.min.css" rel="stylesheet">
</head>
<body background="https://api.ixiaowai.cn/api/api.php">
<div class="container"><br>
    <div class="row">
        <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 center-block text-center" style="float: none;">
            <div class="alert alert-success" role="alert"><center><span class="label label-info">亮而不躁</span></a> <span class="label label-primary">丽而不曲</span> </a> <span class="label label-danger">简介明了</span></a> <span class="label label-warning">纯净绿色</span></a>  <span class="label label-warning">安全稳定</span></center></div>
            
        </div>
        <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 center-block" style="float: none;">
<div class="panel panel-primary">

<?php if ($do == '1') { ?>
<div class="panel panel-black">
	<div class="panel panel-primary">
	<div class="panel-heading" style="background: #15A638;">
		<h3 class="panel-title" align="center">BR授权系统</h3>
	</div>
	<div class="panel-body">
	<img src="http://auth1.yq.meigui66.com/csa.png" alt="img"; width=99% height=99%><p><p><p><font color="#00FFFF">不</font><font color="#05FAFF">妥</font><font color="#0AF5FF">协</font><font color="#0FF0FF">，</font><font color="#14EBFF">不</font><font color="#19E6FF">逐</font><font color="#1EE1FF">流</font><font color="#23DCFF">；</font><font color="#28D7FF">随</font><font color="#2DD2FF">性</font><font color="#32CDFF">而</font><font color="#37C8FF">不</font><font color="#3CC3FF">失</font><font color="#41BEFF">个</font><font color="#46B9FF">性</font><font color="#4BB4FF">，</font><font color="#50AFFF">有</font><font color="#55AAFF">设</font><font color="#5AA5FF">计</font><font color="#5FA0FF">而</font><font color="#649BFF">不</font><font color="#6996FF">漏</font><font color="#6E91FF">痕</font><font color="#738CFF">迹</font><font color="#7887FF">；</font><font color="#7D82FF">缘</font><font color="#827DFF">浅</font><font color="#8778FF">易</font><font color="#8C73FF">支</font><font color="#916EFF">付</font><font color="#9669FF">，</font><font color="#9B64FF">繁</font><font color="#A060FF">华</font><font color="#A560FF">阅</font><font color="#AA60FF">尽</font><font color="#AF60FF">处</font><font color="#B460FF">，</font><font color="#B960FF">简</font><font color="#BE60FF">约</font><font color="#C360FF">不</font><font color="#C860FF">简</font><font color="#CD60FF">单</font><font color="#D260FF">！</font>
<?php if ($installed) { ?>
		<div class="alert alert-warning">您已经安装过本系统，如需重新安装请删除<font color=red> “install/install.lock” </font>文件后再安装！</div>
		<?php }else{?>
		<p align="center"><a class="btn btn-primary" href="?do=2">开始安装</a></p>
		<?php }?>
	</div>
</div>



<?php }elseif($do=='2'){?>
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title" align="center">环境检查</h3>
	</div>
<table class="table table-striped">
	<thead>
		<tr>
			<th style="width:20%">函数检测</th>
			<th style="width:15%">需求</th>
			<th style="width:15%">当前</th>
			<th style="width:50%">用途</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>PHP 7.0+</td>
			<td>必须</td>
			<td><?php echo version_compare(PHP_VERSION, '7.0.0', '>')?'<font color="green">'.PHP_VERSION.'</font>':'<font color="red">'.PHP_VERSION.'</font>'; ?></td>
			<td>PHP版本支持</td>
		</tr>
		<tr>
			<td>curl_exec()</td>
			<td>必须</td>
			<td><?php echo checkfunc('curl_exec',true); ?></td>
			<td>抓取网页</td>
		</tr>
		<tr>
			<td>file_get_contents()</td>
			<td>必须</td>
			<td><?php echo checkfunc('file_get_contents',true); ?></td>
			<td>读取文件</td>
		</tr>
		<tr>
			<td>session</td>
			<td>必须</td>
			<td><?php echo $_SESSION['checksession']==1?'<font color="green">可用</font>':'<font color="red">不支持</font>'; ?></td>
			<td>PHP必备功能</td>
		</tr>
	</tbody>
</table>
<p><span><a class="btn btn-primary" href="index.php?do=1"><<上一步</a></span>
<span style="float:right"><a class="btn btn-primary" href="index.php?do=3" align="right">下一步>></a></span></p>
</div>
<?php }elseif($do=='3'){?>
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title" align="center">数据库配置</h3>
	</div>
	<div class="panel-body">
	<?php
if(defined("SAE_ACCESSKEY"))
echo <<<HTML
检测到您使用的是SAE空间，支持一键安装，请点击 <a href="?do=4">下一步</a>
HTML;
else
echo <<<HTML
		<form action="?do=4" class="form-group" method="post">
		<label for="name">数据库地址:</label>
		<input type="text" class="form-control" name="db_host" value="localhost">
		<label for="name">数据库端口:</label>
		<input type="text" class="form-control" name="db_port" value="3306">
		<label for="name">数据库用户名:</label>
		<input type="text" class="form-control" name="db_user">
		<label for="name">数据库密码:</label>
		<input type="text" class="form-control" name="db_pwd">
		<label for="name">数据库名:</label>
		<input type="text" class="form-control" name="db_name">
		<br><input type="submit" class="btn btn-primary btn-block" name="submit" value="保存配置">
		</form><br/>
	
  <center>如果已事先填写好config.php文件中相关数据库配置，请  <a href="?do=4&jump=1">点击此处</a> 跳过这一步。<br><font color="red">安装后倘若运行出错，请切换PHP版本至正常运行为止！</font><br></center>
HTML;
?>
	</div>
</div>

<?php }elseif($do=='4'){
?>
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title" align="center">保存数据库</h3>
	</div>
	<div class="panel-body">
<?php
require './db.class.php';
if(defined("SAE_ACCESSKEY") || $_GET['jump']==1){
	include_once '../config.php';
	if(!$dbconfig['user']||!$dbconfig['pwd']||!$dbconfig['dbname']) {
		echo '<div class="alert alert-danger">请先填写好数据库并保存后再安装！<hr/><a href="javascript:history.back(-1)"><< 返回上一页</a></div>';
	} else {
		if(!$con=DB::connect($dbconfig['host'],$dbconfig['user'],$dbconfig['pwd'],$dbconfig['dbname'],$dbconfig['port'])){
			if(DB::connect_errno()==2002)
				echo '<div class="alert alert-warning">连接数据库失败，数据库地址填写错误！</div>';
			elseif(DB::connect_errno()==1045)
				echo '<div class="alert alert-warning">连接数据库失败，数据库用户名或密码填写错误！</div>';
			elseif(DB::connect_errno()==1049)
				echo '<div class="alert alert-warning">连接数据库失败，数据库名不存在！</div>';
			else
				echo '<div class="alert alert-warning">连接数据库失败，['.DB::connect_errno().']'.DB::connect_error().'</div>';
		}else{
			echo '<div class="alert alert-success">数据库配置文件保存成功！</div>';
			if(DB::query("select * from ".$dbconfig['dbqz']."_config where 1")==FALSE)
				echo '<p align="right"><a class="btn btn-primary btn-block" href="?do=5">创建数据表>></a></p>';
			else
				echo '<div class="list-group-item list-group-item-info">系统检测到你已安装过Birth授权系统</div>
				<div class="list-group-item">
					<a href="?do=7" class="btn btn-block btn-info">跳过安装</a>
				</div>
				<div class="list-group-item">
					<span onclick="layer.confirm(\'全新安装将会清空所有数据，是否继续？\',{title:\'提示\',btn: [\'确定\',\'取消\']}, function(){window.location.href = \'?do=5\';});" class="btn btn-block btn-warning">强制全新安装</span>
				</div>';
		}
	}
}else{
	$db_host=isset($_POST['db_host'])?$_POST['db_host']:NULL;
	$db_port=isset($_POST['db_port'])?$_POST['db_port']:NULL;
	$db_user=isset($_POST['db_user'])?$_POST['db_user']:NULL;
	$db_pwd=isset($_POST['db_pwd'])?$_POST['db_pwd']:NULL;
	$db_name=isset($_POST['db_name'])?$_POST['db_name']:NULL;
	$db_qz=isset($_POST['db_qz'])?$_POST['db_qz']:'authguao';
	if ($db_host==null || $db_port==null || $db_user==null || $db_pwd==null || $db_name==null || $db_qz==null){
		echo '<div class="alert alert-danger">保存错误,请确保每项都不为空<hr/><a href="javascript:history.back(-1)"><< 返回上一页</a></div>';
	} else {
		$config="<?php
/*数据库配置*/
\$dbconfig=array(
	'host' => '{$db_host}', //数据库服务器
	'port' => {$db_port}, //数据库端口
	'user' => '{$db_user}', //数据库用户名
	'pwd' => '{$db_pwd}', //数据库密码
	'dbname' => '{$db_name}', //数据库名
	'dbqz' => '{$db_qz}' //数据表前缀
);
?>";
		if(!$con=DB::connect($db_host,$db_user,$db_pwd,$db_name,$db_port)){
			if(DB::connect_errno()==2002)
				echo '<div class="alert alert-warning">连接数据库失败，数据库地址填写错误！</div>';
			elseif(DB::connect_errno()==1045)
				echo '<div class="alert alert-warning">连接数据库失败，数据库用户名或密码填写错误！</div>';
			elseif(DB::connect_errno()==1049)
				echo '<div class="alert alert-warning">连接数据库失败，数据库名不存在！</div>';
			else
				echo '<div class="alert alert-warning">连接数据库失败，['.DB::connect_errno().']'.DB::connect_error().'</div>';
		}elseif(file_put_contents('../config.php',$config)){
			if(function_exists("opcache_reset"))@opcache_reset();
			echo '<div class="alert alert-success">数据库配置文件保存成功！</div>';
			if(DB::query("select * from ".$db_qz."_config where 1")==FALSE)
				echo '<p align="right"><a class="btn btn-primary btn-block" href="?do=5">创建数据表>></a></p>';
			else
				echo '<div class="list-group-item list-group-item-info">系统检测到你已安装过Birth授权系统</div>
				<div class="list-group-item">
					<a href="?do=7" class="btn btn-block btn-info">跳过安装</a>
				</div>
				<div class="list-group-item">
					<span onclick="layer.confirm(\'全新安装将会清空所有数据，是否继续？\',{title:\'提示\',btn: [\'确定\',\'取消\']}, function(){window.location.href = \'?do=5\';});" class="btn btn-block btn-warning">强制全新安装</span>
				</div>';
		}else
			echo '<div class="alert alert-danger">保存失败，请确保网站根目录有写入权限<hr/><a href="javascript:history.back(-1)"><< 返回上一页</a></div>';
	}
}
?>
	</div>
</div>
<?php }elseif($do=='5'){?>
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title" align="center">创建数据表</h3>
	</div>
	<div class="panel-body">
<?php
include_once '../config.php';
if(!$dbconfig['user']||!$dbconfig['pwd']||!$dbconfig['dbname']) {
	echo '<div class="alert alert-danger">请先填写好数据库并保存后再安装！<hr/><a href="javascript:history.back(-1)"><< 返回上一页</a></div>';
} else {
	require './db.class.php';
	$sql=file_get_contents("install.sql");
	$sql=explode(';',$sql);
	$cn = DB::connect($dbconfig['host'],$dbconfig['user'],$dbconfig['pwd'],$dbconfig['dbname'],$dbconfig['port']);
	if (!$cn) die('err:'.DB::connect_error());
	DB::query("set sql_mode = ''");
	DB::query("set names utf8");
	$t=0; $e=0; $error='';
	for($i=0;$i<count($sql);$i++) {
		if ($sql[$i]=='')continue;
		if(DB::query($sql[$i])) {
			++$t;
		} else {
			++$e;
			$error.=DB::error().'<br/>';
		}
	}
	date_default_timezone_set("PRC");
	DB::query("INSERT INTO `authguao_config` VALUES ('syskey', '".random(32)."')");
}
if($e==0) {
	echo '<div class="alert alert-success">安装成功！<br/>SQL成功'.$t.'句/失败'.$e.'句</div><p align="right"><a class="btn btn-block btn-primary" href="index.php?do=6">下一步>></a></p>';
} else {
	echo '<div class="alert alert-danger">安装失败<br/>SQL成功'.$t.'句/失败'.$e.'句<br/>错误信息：'.$error.'</div><p align="right"><a class="btn btn-block btn-primary" href="index.php?do=5">点此进行重试</a></p>';
}
?>
	</div>
</div>

<?php }elseif($do=='6'){?>
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title" align="center">安装完成</h3>
	</div>
	<div class="panel-body">
<?php
	@file_put_contents("install.lock",'安装锁');
	echo '<div class="alert alert-info"><font color="White">安装完成！管理账号和密码是:admin/123456</font><br/><br/><a href="../">>>网站首页</a>｜<a href="../admin/">>>后台管理</a><hr/>更多设置选项请登录后台管理进行修改。<br/><br/><font color="#FF0033">如果你的空间不支持本地文件读写，请自行在install/ 目录建立 install.lock 文件！</font></div>';
?>
	</div>
</div>

<?php }elseif($do=='7'){?>
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title" align="center">安装完成</h3>
	</div>
	<div class="panel-body">
<?php
	@file_put_contents("install.lock",'安装锁');
	echo '<div class="alert alert-info"><font color="White">安装完成！管理账号和密码是:admin/123456</font><br/><br/><a href="../">>>网站首页</a>｜<a href="../admin/">>>后台管理</a><hr/>更多设置选项请登录后台管理进行修改。<br/><br/><font color="#FF0033">如果你的空间不支持本地文件读写，请自行在install/ 目录建立 install.lock 文件！</font></div> ';
?>
	</div>
</div>

<?php }?>

</div>
</body>
</html>
<?php }?>